//
//  NormalTreeCell.swift
//  ANOA
//
//  Created by 邱仙凯 on 2024/1/18.
//  Copyright © 2024 zwb. All rights reserved.
//

import UIKit

import SnapKit
class NormalTreeCell: UITableViewCell {
    
    var left1 :UILabel = UILabel()
    var btn = BigRangeButton()
    var btnClosure:(()->())?
    var selectBtnClosure:(()->())?
    var selectBtn = BigRangeButton()
    var showParam = ""
    var one:NormalTreeNode? {
        didSet {
           
            left1.textColor =  one!.color
            
            let v = any2Str(one!.dic[showParam])

            left1.text = v
            
            left1.snp.remakeConstraints { (make) in
                make.left.equalTo(self).offset(33 + one!.level.intWrap * 19)
                make.top.equalTo(self).offset(8)
            }
            if one!.children.isSome {
                left1.font = UIFont.systemFont(ofSize: 17)
                if one!.open {
                    btn.setImage(UIImage(named:"cellopen"), for: .normal)
                } else {
                    btn.setImage(UIImage(named:"cellclose"), for: .normal)
                }
                
            } else {
                left1.font = UIFont.systemFont(ofSize: 14)
                btn.setImage(nil, for: .normal)
            }
            if one!.type == .single {
                selectBtn.isHidden = true
            } else {
                selectBtn.isHidden = false
                if one!.children.isSome {
                    selectBtn.setImage(one!.selected ? UIImage(named:"xuanze-2") : UIImage(named:"xuanze"), for: .normal)
                } else {
                    selectBtn.setImage(one!.selected ? UIImage(named:"select") : UIImage(named:"unselect"), for: .normal)
                }
                
            }
            
            
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.contentView.addSubview(left1)
        left1.snp.makeConstraints { (make) in
            make.left.equalTo(self).offset(45)
            make.top.equalTo(self).offset(8)
        }
        self.contentView.addSubview(btn)
        btn.addTarget(self, action: #selector(btnAction), for: .touchUpInside)
        
        btn.snp.makeConstraints { (make) in
            make.right.equalTo(left1.snp.left).offset(-5)
            make.width.height.equalTo(20)
            make.centerY.equalTo(self)
        }
        self.contentView.addSubview(selectBtn)
        selectBtn.addTarget(self, action: #selector(selectBtnAction), for: .touchUpInside)
        
        selectBtn.snp.makeConstraints { (make) in
            make.right.equalTo(self.contentView).offset(-10)
            make.width.height.equalTo(25)
            make.centerY.equalTo(self)
        }
        
        
    }
    @objc func selectBtnAction() {
        if let clo = self.selectBtnClosure {
            clo()
        }
    }
    @objc func btnAction() {
        if let clo = self.btnClosure {
            clo()
        }
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }
    
}
